﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QAAdmin.BusinessObject;

namespace QAAdmin.UI
{
    public partial class frmUser : BaseForm
    {
        public enum Mode
        {
            New,
            Modify
        }

        private Mode mode;

        public frmUser(Mode mode)
        {
            InitializeComponent();
            this.mode = mode;

            if (mode != Mode.New)
            {
                txtPassword.Enabled = false;
                txtUsername.ReadOnly = true;
            }

        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            if(cmbGroup.SelectedIndex < 0)
                cmbGroup.SelectedIndex = 0;
        }

        private bool ValidateMe()
        {
            if (txtUsername.Text.Length == 0)
                return ShowErrorMessage("Username tidak boleh kosong");
            if (mode == Mode.New && QAController.IsUsernameExist(txtUsername.Text))
                return ShowErrorMessage("Username \"" + txtUsername.Text + "\" sudah ada di dalam database");
            return true;
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                if (!ValidateMe())
                    return;

                if (mode == Mode.New)
                {
                    Petugas p = new Petugas();
                    p.Username = txtUsername.Text;
                    p.Password = txtPassword.Text;
                    p.Group = cmbGroup.SelectedItem.ToString();
                    if (AD.ValidateUserWithAD(p.Username, p.Password)) QAController.BuatPetugas(p);
                    else throw new Exception("User tidak terdaftar di active directory");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else if (mode == Mode.Modify)
                {
                    Petugas p = new Petugas();
                    p.Username = txtUsername.Text;
                    if (txtPassword.Enabled)
                        p.Password = txtPassword.Text;
                    else
                        p.Password = String.Empty;
                    p.Group = cmbGroup.SelectedItem.ToString();
                    p.IsLogin = chkLogin.Checked;
                    QAController.UbahPetugas(p);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private bool ShowErrorMessage(string message)
        {
            MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
    }
}
